<template>
  <div class="edit">
    <!-- 顶部 -->
    <van-nav-bar title="编辑资料" left-arrow @click-left="$router.go(-1)" />
    <!-- 头像 -->
    <div class="Avatar">
      <van-image
        width="120"
        height="120"
        round
        fit="cover"
        :src="userInfo.photo"
      />
      <van-uploader :after-read="afterRead" />
    </div>
    <van-cell-group inset>
      <van-cell
        title="名称"
        :value="userInfo.name"
        is-link
        @click="editNameClick"
      />
      <van-cell
        title="性别"
        :value="userInfo.gender ? '女' : '男'"
        is-link
        @click="isGender = true"
      />
      <van-cell
        title="生日"
        :value="userInfo.birthday"
        is-link
        @click="isDate = true"
      />
    </van-cell-group>
    <!-- 编辑姓名弹窗 -->
    <van-dialog
      v-model="isShow"
      title="编辑姓名"
      show-cancel-button
      @confirm="upDataInfo"
    >
      <van-field ref="input" v-model="newName" placeholder="请输入用户名" />
    </van-dialog>
    <!-- 编辑性别弹窗 -->
    <van-popup v-model="isGender" position="bottom">
      <van-nav-bar title="编辑性别" left-arrow @click-left="isGender = false" />
      <van-cell title="男" is-link @click="editGender(0)" />
      <van-cell title="女" is-link @click="editGender(1)" />
    </van-popup>

    <!-- 弹出框 -->
    <van-popup v-model="isDate" position="bottom" ref="newdata">
      <!-- 日期弹窗 -->
      <van-datetime-picker
        v-model="currentDate"
        type="date"
        title="选择年月日"
        :min-date="minDate"
        :max-date="maxDate"
        @cancel="isDate = false"
        @confirm="changeDate"
      />
    </van-popup>

    <!-- 图片裁切 -->
    <div class="cropper" v-show="showCropper">
      <vueCropper
        ref="cropper"
        :img="img"
        autoCrop
        autoCropWidth="120"
        autoCropHeight="120"
      ></vueCropper>
      <van-button type="primary" @click="showCropper = false">取消</van-button>
      <van-button type="info" @click="upDataPhoto">确认</van-button>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
import { editUserInfo } from "./user";
import { editUserPhoto } from "./user";
import * as moment from "moment";
import { VueCropper } from "vue-cropper";

export default {
  components: {
    VueCropper,
  },
  name: "edit",
  data() {
    return {
      isShow: false,
      newName: "",
      newDate: "",
      isGender: false,
      isDate: false,
      minDate: new Date(1900, 0, 1),
      maxDate: new Date(),
      currentDate: "",
      img: "",
      showCropper: false,
    };
  },
  methods: {
    editNameClick() {
      this.isShow = true;
      this.newName = this.userInfo.name;
      // vue API 里的回调函数，是一个异步，相当于是高级的定时器
      this.$nextTick(function () {
        this.$refs.input.focus();
      });
    },
    upDataInfo() {
      editUserInfo({ name: this.newName }).then((res) => {
        // console.log(res);
        this.$toast.success("更改成功");
        this.$store.commit("updataUserInfo", {
          ...this.userInfo,
          name: this.newName,
        });
      });
    },
    editGender(gender) {
      editUserInfo({ gender }).then((res) => {
        // console.log(res);
        this.$toast.success("更改成功");
        this.$store.commit("updataUserInfo", {
          ...this.userInfo,
          gender,
        });
        this.isGender = false;
      });
    },
    changeDate() {
      this.isDate = false;
      // console.log(this.currentDate);
      let birthday = moment(this.currentDate).format("YYYY - MM - DD");
      console.log(birthday);
      editUserInfo({ birthday }).then((res) => {
        this.$toast.success("更新成功");
        this.$store.commit("updataUserInfo", {
          ...this.userInfo,
          birthday,
        });
      });
    },
    afterRead(file) {
      // 此时可以自行将文件上传至服务器
      console.log(file);
      this.img = file.content;
      this.showCropper = true;
      // let fd = new FormData();
      // fd.append("photo", file.file);
      // editUserPhoto(fd).then((res) => {
      //   console.log(res);
      //   this.$toast.success("更改成功");
      //   this.$store.commit("updataUserInfo", {
      //     ...this.userInfo,
      //     photo: res.data.data.photo,
      //   });
      // });
    },
    upDataPhoto() {
      this.$refs.cropper.getCropBlob(async (data) => {
        let fd = new FormData();
        fd.append("photo", data);
        await editUserPhoto(fd);
        this.$toast.success("更改成功");
        this.showCropper = false;
        this.$store.commit("updataUserInfo", {
          ...this.userInfo,
          photo: res.data.data.photo,
        });
        // editUserPhoto(fd).then((res) => {
        //   console.log(res);
        //   this.$toast.success("更改成功");
        //   this.showCropper = false;
        //   this.$store.commit("updataUserInfo", {
        //     ...this.userInfo,
        //     photo: res.data.data.photo,
        //   });
        // });
      });
    },
  },
  computed: {
    ...mapState(["userInfo"]),
  },
};
</script>

<style lang="less">
.edit {
  .van-nav-bar__content {
    background-color: #1989fa;
    .van-icon-arrow-left {
      color: #fff;
    }
    .van-nav-bar__title {
      color: #fff;
    }
  }
  .Avatar {
    display: flex;
    height: 150px;
    justify-content: center;
    align-items: center;
    .van-uploader {
      position: absolute;
      width: 120px;
      height: 120px;
      opacity: 0;
      .van-uploader__upload {
        width: 120px;
        height: 120px;
      }
    }
  }
  .cropper {
    position: fixed;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    .van-button--primary {
      position: absolute;
      left: 0;
      bottom: 0;
    }
    .van-button--info {
      position: absolute;
      right: 0;
      bottom: 0;
    }
  }
}
</style>